package sanphone.com.runningschool.Saying;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;


import sanphone.com.runningschool.JuShou.JuShouDetailActivity;
import sanphone.com.runningschool.R;
import sanphone.com.runningschool.entity.Question;

public class SayingListViewAdapter extends BaseAdapter {
    private Context mContext;
    private List<Question> saying_info;

    public SayingListViewAdapter(Context context, List<Question> infos) {
        mContext = context;
        saying_info = infos;
    }


    @Override
    public int getCount() {
        return saying_info.size();
    }

    @Override
    public Object getItem(int position) {
        return saying_info.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.saying_listview_item, null);
            holder.iconImage = view.findViewById(R.id.paotui_publisher_icon);
            holder.detailText = view.findViewById(R.id.discussion);
            holder.titleText=view.findViewById(R.id.s_title);
            holder.nameText = view.findViewById(R.id.paotui_publisher_name);
            holder.contentText = view.findViewById(R.id.paotui_publisher_content);
            holder.day=view.findViewById(R.id.saying_day);
            holder.month=view.findViewById(R.id.saying_month);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        //控件和数据之间进行绑定
        final Question q = saying_info.get(position);
        Picasso.with(mContext).load(R.drawable.icon)
                .placeholder(R.mipmap.ic_launcher)
                .resize(60, 60)
                .onlyScaleDown()
                .into(holder.iconImage);
        holder.nameText.setText(q.getNickName());
        holder.titleText.setText(q.getQuestionTitle());
        holder.month.setText(q.getCreatedTime().substring(5,7)+"月");
        holder.day.setText(q.getCreatedTime().substring(8,10));
        String content=null,title= null;
        try {
            content = URLDecoder.decode(q.getQuestionText(),"UTF-8");
            title=URLDecoder.decode(q.getQuestionTitle(),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        holder.contentText.setText(content);
        holder.titleText.setText(title);
        final String finalContent = content;
        final String finalTitle = title;
        holder.detailText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext,SayingDetailActivity.class);
                intent.putExtra("userName",q.getNickName());
                intent.putExtra("context", finalContent);
                intent.putExtra("questionId",q.getQuestionId());
                intent.putExtra("questionTitle", finalTitle);
                view.getContext().startActivity(intent);

            }
        });
        holder.contentText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,SayingDetailActivity.class);
                intent.putExtra("userName",q.getNickName());
                intent.putExtra("context", finalContent);
                intent.putExtra("questionId",q.getQuestionId());
                intent.putExtra("questionTitle",finalTitle);
                v.getContext().startActivity(intent);
            }
        });
        return view;
    }

    class ViewHolder {
        ImageView iconImage;
        TextView nameText, contentText,detailText,titleText,day,month;
    }

}




